/*
 * 组织界面验证js
 * 依赖组件[bootstrapValidator]
 */
(function() {
	require(['jquery', 'vbox', 'box', 'select2'], function(jquery, vbox, box) {
		if(typeof $.fn.bootstrapValidator === 'undefined') {	//检测bootstrapValidator依赖项
			throw new Error('bootstrapValidator依赖项未能加载');
		}
		
		/*标识符*/
		var formData = [{id:"0", text:"省局"}, {id:"1", text:"司局"},  {id:"9", text:"其他"}];
		$("#update_type").select2({
			 placeholder: "机构类别",
			 allowClear: true,
			 data: formData,
			 minimumResultsForSearch: Infinity
		 }).val($('#subContent').data('row').identify).trigger("change");
		
		/*组织代码失去焦点事件*/
		$('#update_code').blur(function (){ 
			var code = $('#update_code').val();
			var id = $('#subContent').data('row').id;
			$.ajax({
				/*url路径*/
				url: 'organization/validateOrganizationInfo.service',
				type: 'post',
				async: false,
				data: {
					id: id,
					code: code
					},
				success: function(data) {
					if(data.errorCode == "1004"){
						var modalObj = box.repeatModal('org_update'),
			        	modalId = modalObj.modalId, modalBodyId = modalObj.modalBodyId, btnSure = modalObj.btnSure;
			        	var repeat_modal = $('#' + modalId), 
						btn_sure = $('#' + btnSure);
			        	$('#' + modalBodyId).html('机构代码最好不要重复！！');
			        	repeat_modal.modal();
			        	btn_sure.unbind("click").bind("click", function() {
			        		repeat_modal.modal('hide');
			        	});
					}
				}
			});
		});
		
		var v = vbox;
		$('#org_update_form').bootstrapValidator({
	        feedbackIcons: v.feedbackIcons,
	        trigger: 'blur',
	        fields: {
	        	name: {
	                validators: {
	                    notEmpty: v.notEmpty,
	                    stringLength: v.stringLength500,
	                    regexp: v.regexpName,
	                    callback: {
	                    	message: '机构名重复',
	                        callback: function(value, validator) {
	                        	var flag = false;
	                        	var id =  $('#subContent').data('row').id;
	                        	$.ajax({
	                				url: 'organization/validateOrganizationInfo.service',
	                				type: 'post',
	                				async: false,
	                				data: {
	                					id: id,
	                					name: value
	                					},
	                				success: function(data) {
	                					if(data.errorCode == "1003"){
	                						flag = false;
	                					}else{
	                						flag = true;
	                					}
	                				}
	                			});
	                        	return flag;
	                        }
	                    }
	                }
	            },
	            code: {
	                validators: {
	                	notEmpty: v.notEmpty,
	                    stringLength: v.stringLength100,
	                    regexp: v.regexpCode
	                }
	            },
	            identify: {
	            	validators: {
	            		notEmpty:v.notEmpty
	            	}
	            },
	            description: {
	                validators: {
	                	stringLength: v.stringLength1000
	                }
	            }
	        }
	    });
		
	});
})();